(use srfi-1)
(use util.stream)

(define (scale-stream strm n)
  (stream-map (lambda (x) (* x n)) strm))

(define (partial-sum strm)
  (stream-delay (stream-cons (stream-car strm)
			     (stream-map (lambda (x)
					   (+ x (stream-car strm)))
					 (partial-sum (stream-cdr strm))))))

(define (display-stream s)
  (stream-for-each print (stream-take s 8)))

(define (pi-summands n)
  (stream-delay (stream-cons (/ 1.0 n)
			     (stream-map - (pi-summands (+ n 2))))))

(define pi-stream (scale-stream (partial-sum (pi-summands 1)) 4))

(display-stream pi-stream)

(define (euler-transform s)
  (let ((s0 (stream-ref s 0)) ; Sn-1
	(s1 (stream-ref s 1)) ; Sn
	(s2 (stream-ref s 2))) ; Sn+1
    (stream-delay (stream-cons (- s2 (/ (expt (- s2 s1) 2)
					(+ s0 (* -2 s1) s2)))
			       (euler-transform (stream-cdr s))))))

(display-stream (euler-transform pi-stream))

(define (make-tableau transform s)
  (stream-delay (stream-cons s
			     (make-tableau transform
					   (transform s)))))

;;tableau の行の第1 項目をとって、加速した数列を加速した数列を取り出す。
(define (accelerated-sequence transform s)
  (stream-map stream-car
	      (make-tableau transform s)))

(display-stream (accelerated-sequence euler-transform pi-stream))